<?
require_once('../mod_config.php');

$moduol_name      = 'Administration';
$mod_id           = 5;

$table            = 'administrator';
$adt_id           = 'adt_id';
$adt_username     = 'adt_username';

$user_id          = getValue("user_id","int","SESSION");

/*-------------------- Class Module -----------------------*/
class Module{
   
      public static function getListModule($limit =''){
         if($limit != '') $limit = 'LIMIT ' . $limit;
         $sql = "SELECT 
                           * 
                        FROM
                           modules
                        ORDER BY
                           mod_order
                        " . $limit;
         $db = new DB();
         $arr_module = $db->query($sql);
         return $arr_module;
      }
      
      public static function getInfo($mod_id){
         $arr_info = array();
         $sql = "SELECT 
                           * 
                        FROM
                           modules
                        WHERE
                           mod_id = ".$mod_id."
                        LIMIT 1";
         $db = new DB();
         $arr_info = $db->row($sql);         
         return $arr_info;
      }
      public static function getTotal(){
         $total = 0;
         $sql = "SELECT count(mod_id) as total FROM modules";
         $db = new DB();
         $total = $db->single($sql);
         return $total;
      }
      
      public static function deleteModule($mod_id){
         $db = new DB();
         $db->query("DELETE FROM modules WHERE mod_id =" . $mod_id);
      }
      
      public static function addModule($mod_name,$mod_dir,$mod_file,$mod_sub,$mod_order,$mod_active =1){
         $str_insert = "INSERT INTO 
                                 modules(mod_name,
                                         mod_dir,
                                         mod_file,
                                         mod_sub,
                                         mod_order,
                                         mod_active)
                                 VALUES('" . $mod_name . "',
                                        '" . $mod_dir . "',
                                        '" . $mod_file . "',
                                        '" . $mod_sub . "',
                                         " . $mod_order . ",
                                         " . $mod_active .")";
         $db = new DB();
         $inserted = $db->query($str_insert);
         
         //Create foder
         $path_to_folder = '../' . $mod_dir;
         if(!is_dir($path_to_folder)){
            mkdir($path_to_folder,755);
         }
         //Create file
         $array_file = explode('|',$mod_file);
         if(count($array_file) > 0){
            foreach($array_file as $value){
               if(!file_exists($path_to_folder.'/'.$value))
                  file_put_contents($path_to_folder.'/'.$value,'<?php ?>');
            }
            file_put_contents($path_to_folder.'/'.$value,"<?php require ('../mod_config.php'); ?>");
         }
         unset($db_insert);
         return $inserted;
      }
      
      public static function updateModule($mod_id,$mod_name,$mod_dir,$mod_file,$mod_sub,$mod_order){
         $str_update = "UPDATE
                              modules
                           SET
                              mod_name = '" . $mod_name . "', 
                              mod_dir = '" . $mod_dir . "', 
                              mod_file ='" . $mod_file . "', 
                              mod_sub = '" . $mod_sub . "', 
                              mod_order =" . $mod_order ." 
                           WHERE 
                              mod_id = " . $mod_id;
         $sql  = "UPDATE modules SET mod_name =:name, mod_dir =:dir, mod_file =:file, mod_sub =:sub, mod_order =:order WHERE mod_id =:id";
         $value = array('name' => $mod_name,'dir' => $mod_dir,'file' => $mod_file,'sub' => $mod_sub,'order' => $mod_order,'id' => $mod_id);
         $db = new DB();
         $updated = $db->query($sql,$value);
         
         return $updated;
      }
   }
?>